package com.lysj.bhp.withdraw.controller;


import com.lysj.bhp.investment.domain.Reward;
import com.lysj.bhp.system.repository.VerifyEmailRepository;
import com.lysj.bhp.system.repository.VerifyRepository;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import com.lysj.bhp.withdraw.base.WithdrawContent;
import com.lysj.bhp.withdraw.domain.WithDraw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("admin/withdraw_apply")
public class WithdrawApplyController extends WithdrawContent {
    @Autowired
    VerifyRepository verifyRepository;
    @Autowired
    VerifyEmailRepository verifyEmailRepository;

    private final long NEXT_SEND_EMAIL = 1000 * 60 * 3;// 发送邮件间隔(15分钟)
//    private final long NEXT_SEND_EMAIL = 0;// 发送邮件间隔(用于测试)

    @GetMapping("/list") // 查看申请列表
    public Resp list(WithDraw model) {
        List<WithDraw> withDrawList = withdrawRepository.findByStatusOrderByCreateDateDesc(0);
        for (WithDraw withDraw : withDrawList) {
            withDraw.setActualAmount(withDraw.getAmount().subtract(withDraw.getFeeAmount()));
            User user = userRepository.findByUserId(withDraw.getUserId());
            withDraw.setLockDate(user.getLockDate());
        }
        return new Resp(withDrawList);
    }


    @PostMapping("/transfer_success") // 完成转币
    public Resp transferSuccess(Long withdrawId) {
        if (ParamUtil.isBlack(withdrawId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择正确的提币申请");
        }
        WithDraw withDraw = withdrawRepository.findOne(withdrawId);
        if (withDraw == null) {
            return new Resp(Resp.PARAM_ERROR, "请选择正确的提币申请");
        }
        if (withDraw.getStatus() != 0) {
            return new Resp(Resp.PARAM_ERROR, "该提币申请已经审核过了");
        }
        withDraw.setStatus(1);
        withDraw.setCheckDate(new Date());
        withDraw.setCheckReason("转币成功");
        withDraw.setCheckStatus(1);
        withdrawRepository.save(withDraw);
        //修改用户钱包中的余额总资产跟冻结余额
        switch (withDraw.getType()) {  //10:比特币;20:以太坊;100:BHPC;110:BHPC_Bouns
            case 100:
                BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(withDraw.getUserId());
                bhpcWallet.setFreezeBalance(bhpcWallet.getFreezeBalance().subtract(withDraw.getAmount()));
                bhpcWallet.setBalance(bhpcWallet.getBalance().subtract(withDraw.getAmount()));
                bhpcWalletRepository.save(bhpcWallet);
                break;
            case 20:
                EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(withDraw.getUserId());
                ethereumWallet.setFreezeBalance(ethereumWallet.getFreezeBalance().subtract(withDraw.getAmount()));
                ethereumWallet.setBalance(ethereumWallet.getBalance().subtract(withDraw.getAmount()));
                ethereumWalletRepository.save(ethereumWallet);
                break;
            case 10:
                BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(withDraw.getUserId());
                bitCoinWallet.setFreezeBalance(bitCoinWallet.getFreezeBalance().subtract(withDraw.getAmount()));
                bitCoinWallet.setBalance(bitCoinWallet.getBalance().subtract(withDraw.getAmount()));
                bitcoinWalletRepository.save(bitCoinWallet);
                break;
            case 110:
                //Reward reward = rewardRepository.findByUserId(currentUser().getUserId());
                Reward reward = rewardRepository.findByUserIdAndType(withDraw.getUserId(), 100);
                reward.setFreezeBalance(reward.getFreezeBalance().subtract(withDraw.getAmount()));
                reward.setBalance(reward.getBalance().subtract(withDraw.getAmount()));
                rewardRepository.save(reward);
                break;
            default:
                break;
        }
        return new Resp("确定转币完成成功");
    }

    @PostMapping("/transfer_error") // 转币失败
    public Resp transferError(Long withdrawId, String reason) {
        if (ParamUtil.isBlack(withdrawId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择正确的提币申请");
        }
        WithDraw withDraw = withdrawRepository.findOne(withdrawId);
        if (withDraw == null) {
            return new Resp(Resp.PARAM_ERROR, "请选择正确的提币申请");
        }
        if (withDraw.getStatus() != 0) {
            return new Resp(Resp.PARAM_ERROR, "该提币申请已经审核过了");
        }
        withDraw.setStatus(1);
        withDraw.setCheckDate(new Date());
        withDraw.setCheckStatus(2); // 转币错误
        withDraw.setCheckReason(reason);
        withdrawRepository.save(withDraw);
        User user = userRepository.findOne(withDraw.getUserId());
        if (user == null) {
            return new Resp(Resp.PARAM_ERROR, "系统内部错误!!!");
        }
        //10:比特币;20:以太坊;100:BHPC;110:BHPC_Bouns
        if (withDraw.getType() == 100) {
            BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(withDraw.getUserId());
            bhpcWallet.setFreezeBalance(bhpcWallet.getFreezeBalance().subtract(withDraw.getAmount()));
            bhpcWallet.setAvailBalance(bhpcWallet.getAvailBalance().add(withDraw.getAmount()));
            //          bhpcWallet.setBalance(bhpcWallet.getBalance().add(withDraw.getAmount()).add(withDraw.getFeeAmount()));
            bhpcWalletRepository.save(bhpcWallet);
        } else if (withDraw.getType() == 20) {
            EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(withDraw.getUserId());
            ethereumWallet.setFreezeBalance(ethereumWallet.getFreezeBalance().subtract(withDraw.getAmount()));
            ethereumWallet.setAvailBalance(ethereumWallet.getAvailBalance().add(withDraw.getAmount()));
            //          ethereumWallet.setBalance(ethereumWallet.getBalance().add(withDraw.getAmount()).add(withDraw.getFeeAmount()));
            ethereumWalletRepository.save(ethereumWallet);
        } else if (withDraw.getType() == 10) {
            BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(withDraw.getUserId());
            bitCoinWallet.setFreezeBalance(bitCoinWallet.getFreezeBalance().subtract(withDraw.getAmount()));
            bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().add(withDraw.getAmount()));
            //          bitCoinWallet.setBalance(bitCoinWallet.getBalance().add(withDraw.getAmount()).add(withDraw.getFeeAmount()));
            bitcoinWalletRepository.save(bitCoinWallet);
        } else if (withDraw.getType() == 110) {
            //Reward reward = rewardRepository.findByUserId(currentUser().getUserId());
            Reward reward = rewardRepository.findByUserIdAndType(withDraw.getUserId(), 100);
            reward.setFreezeBalance(reward.getFreezeBalance().subtract(withDraw.getAmount()));
            reward.setAvailBalance(reward.getAvailBalance().add(withDraw.getAmount()));
            rewardRepository.save(reward);
        }
        userRepository.save(user);
        return new Resp("确定转币有误成功");
    }


    @PostMapping("/submit_auto_transaction") // 提交自动转币请求
    public Resp submitAutoTransaction(Long withdrawId) {
        if (ParamUtil.isBlack(withdrawId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择正确的提币申请");
        }
        WithDraw withDraw = withdrawRepository.findOne(withdrawId);
        if (withDraw == null) {
            return new Resp(Resp.PARAM_ERROR, "请选择正确的提币申请");
        }
        if (withDraw.getStatus() != 0) {
            return new Resp(Resp.PARAM_ERROR, "该提币申请已经审核过了");
        }
        if(withDraw.getType() == 10 || withDraw.getType() == 20 ){
            return new Resp(Resp.PARAM_ERROR, "暂不支持该种数字币自动提币");
        }
        boolean result = bhpcTransactionService.submitBhpcTransactionOfPrivkey(withDraw);
        if (!result) {
            return new Resp(Resp.PARAM_ERROR, "提交自动转币失败,请稍候重试或者联系管理员");
        }
        withDraw.setTransctionStatus(1);// 将状态改为已经提交自动转币
        return new Resp("提交成功");
    }




}
